﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

namespace OAS
{
    public partial class ContractInfo : System.Web.UI.Page
    {
        ConTractInfoBLL conTractBLL = new ConTractInfoBLL();
        protected void Page_Load(object sender, EventArgs e)
        {

            //不是回发时执行，只在初始页面才会执行

            if (!Page.IsPostBack)
            {
                BinConTractGridView();
            }
        }

        /// <summary>

        /// 分页控件事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            //分页查询数据

            Search();
        }

        private void BinConTractGridView()
        {
            //工厂下拉框数据源

            FactoryDropDownList.DataSource = conTractBLL.GetFactoryList();
            FactoryDropDownList.DataBind();
            FactoryDropDownList.Items.Insert(0, "");

            Search();

        }

        public void Search()
        {
            var searchEntity = new MixedTaskCenterEntity();

            //获取筛选条件

            if (!string.IsNullOrEmpty(FactoryDropDownList.SelectedValue))
            {
                searchEntity.Factory = Convert.ToString(FactoryDropDownList.SelectedValue);
            }
            if (!string.IsNullOrEmpty(CreateByTextBox.Text))
            {
                searchEntity.CreatedBy = Convert.ToString(CreateByTextBox.Text);
            }
            if (!string.IsNullOrEmpty(ProcessTypeTextBox.Text))
            {
                searchEntity.ProcessType = Convert.ToString(ProcessTypeTextBox.Text);
            }
            if (!string.IsNullOrEmpty(ForeignFactoryTextBox.Text))
            {
                searchEntity.ForeignFactory = Convert.ToString(ForeignFactoryTextBox.Text);
            }
            if (!string.IsNullOrEmpty(ContractNoTextBox.Text))
            {
                searchEntity.ContractNo = Convert.ToString(ContractNoTextBox.Text);
            }
            if (!string.IsNullOrEmpty(StartTimeTextBox.Text))
            {
                searchEntity.StartDateTime = Convert.ToDateTime(StartTimeTextBox.Text);
            }
            if (!string.IsNullOrEmpty(EndTimeTextBox.Text))
            {
                searchEntity.EndDateTime = Convert.ToDateTime(EndTimeTextBox.Text);
            }

            string _statusStr = GetStatusString();

            var totalCount = 0;
            //获取分页数据

            var pageData = conTractBLL.GetContract(searchEntity, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount, _statusStr);
            //获取并设置总条数据

            AspNetPager1.RecordCount = totalCount;
            //设置数据源

            ConTractGridView.DataSource = null;
            ConTractGridView.DataSource = pageData;
            ConTractGridView.DataBind();
        }

        public string GetStatusString()
        {
            var _statusString = "";
            if (CH_Status.Items[CH_Status.Items.Count - 1].Selected)
            {
                _statusString = "1,7,8,3,9,5,2,4,6";
            }
            else
            {
                for (int i = 0; i < CH_Status.Items.Count; i++)
                {
                    if (CH_Status.Items[i].Selected)
                    {
                        if (_statusString == "")
                        {
                            _statusString = CH_Status.Items[i].Value;
                        }
                        else
                        {
                            _statusString += "," + CH_Status.Items[i].Value;
                        }

                    }
                }
            }

            return _statusString;
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            //设置当前页为第一页

            AspNetPager1.CurrentPageIndex = 0;
            Search();
        }
    }
}
